package com.foreknow.dao.impl;

import com.foreknow.bean.Guest;
import com.foreknow.dao.GuestDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.SQLException;
import java.util.List;

public class GuestDaoImpl extends BaseDao implements GuestDao {
    @Override
    public Guest getQueryById(Integer id) {
        String sql = "select * from guest where id = ?";
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUEST_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping,id);
        if(list.size()>0){
            return (Guest) list.get(0);
        }
        return null;
    }

    @Override
    public boolean update(Guest guest)throws SQLException {
        String sql = "update guest set name=?,phone=?,email=?,title=?,content=?,createdTime=? where id=?";
        int i = jdbcTemplate.update(sql,guest.getName(),guest.getPhone(),guest.getEmail(),guest.getTitle(),guest.getContent(),guest.getCreatedTime(),guest.getId());
        return i == 1;
    }

    @Override
    public List<Object> getQueryAll() {
        String sql = "select * from guest";
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUEST_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping);
        return list;
    }

    @Override
    public boolean add(Guest guest) throws SQLException {
        String sql = "insert into guest(name,phone,email,title,content,createdTime) values(?,?,?,?,?,?)";
        int i = jdbcTemplate.update(sql,guest.getName(),guest.getPhone(),guest.getEmail(),guest.getTitle(),guest.getContent(),guest.getCreatedTime());
        return i == 1;
    }
}
